<template>
	<view>
		<view class="padding-xl ">
			<!-- 发布成品时候，发布的地址填写这个
			https://www.enpingshow.com/front/diansheng/register/ -->
<!-- 			<button class="cu-btn block bg-green lg margin-top" @tap="gz">观众参展</button>
			<button class="cu-btn block bg-orange lg margin-top" @tap="company">公司参展</button> -->
			
			<form v-show="true">
				<view class="cu-form-group form-company">
					<view class="title">参展商报名</view>
					<view class="content">请输入公司ID 或 参展人员绑定码用于报名登记。</view>
					<input placeholder="请输入公司ID / 参展人员绑定码" name="input" :value="companyCode" @input="changeInput"></input>
					<button class='cu-btn bg-orange shadow' @tap="gotogs">验证</button>
				</view>
			</form>

			<!-- <button class="cu-btn block bg-green lg margin-top" @tap="test">测试1</button> -->

		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				openid: '',
				iscompany: false,
				companyCode: '',
				url: 'https://www.enpingshow.com/front/diansheng/api/',
			}
		},

		onLoad(option) {
			console.log(option)
			// 判断用户是否为观众人员，如果是重定向到观众参展申请表格
			if(option.iscompany == 'false') {
				this.getByOpenid2(option.openid);
			}else{
				if (option.openid) {
					this.openid = option.openid
					this.getByOpenid(this.openid);
				}
			}

		},
		onShow() {

		},
		methods: {
			changeInput(e) {
				this.companyCode = e.target.value;
			},

			company() {
				this.iscompany = !this.iscompany;
			},
			
			
			// 用户为公司人员，通过其 openid 获取信息
			getByOpenid(openid) {
				let that = this;
				uni.request({
					url: this.url + 'getMember.php',
					data: {
						openid: openid
					},
					method: 'POST',
					success: (res) => {
						console.log("res code:"+res.data.code)
						console.log("res data:"+res.data.data.length)
						console.log("res data:"+res.data.data.name)
						
						// 判断该人员是否以填写信息
						if (res.data.code == '1' ) {
							const companyID = res.data.data["company_id"]
							// 若该人员已存在 company_id ，直接跳转到二维码页面
							if(this.checkStringEmpty(companyID.trim())) {
								uni.redirectTo({
									url: '../qrcode/qrcode?phone=' + res.data.data["phone"] +
										'&openid=' + this.openid + '&mid=' + res.data.data["mid"] + '&companyCode=' + companyID + '&iscompany=false'
								});
							}
							
						}
						else
						{
							console.log("no member")
							console.log("res"+res.data)
							console.log("res"+res.code)
						}
					}
				});
			},
			// 普通观众
			//获取信息之后，就去到二维码或者新登记信息
			getByOpenid2(openid) {
				uni.request({
					url: this.url + 'getMember.php',
					data: {
						openid: openid
					},
					method: 'POST',
					success: (res) => {
						// 若已登记信息，直接到二维码页面
						if (res.data.code == '0' && res.data.data != null && res.data.data.length > 0) {
							// 判断该人员是否存在 company_id
							const company_id = res.data.data[0].company_id
							if(this.checkStringEmpty(company_id.trim())) {
								uni.redirectTo({
									url: '../qrcode/qrcode?phone=' + res.data.data[0]["phone"] +
										'&openid=' + openid + '&mid=' + res.data.data[0]["mid"] + '&companyCode=' + res.data.data[0]["company_id"] + '&iscompany=false'
								});
							}else{
								uni.redirectTo({
									url: '../qrcode/qrcode?phone=' + res.data.data[0]["phone"] +
										'&openid=' + openid + '&mid=' + res.data.data[0]["mid"] + '&iscompany=true'
								});
							}
						}else{
							// 没有登记过信息的，跳到表单页
							uni.redirectTo({
								url: `/pages/checkin/checkin?openid=${openid}`
							})
						}
					}
				});
			},
			gotogs(e) {
				if (this.companyCode.length < 4) {
					uni.showToast({
						title: '公司id不少于4位',
						icon: 'none',
						duration: 2000
					});
					return;
				}

                console.log("触发"); 


				uni.request({
					url: this.url + 'getCompany.php',
					data: {
						companyId: this.companyCode
					},
					method: 'POST',
					success: (res) => {
						console.log('获取公司信息:', res)
						if (res.data.code == '0') {
							const {name, booth } = res.data.data[0]
							uni.reLaunch({
								url: '../checkin/checkin?openid=' + this.openid + '&companyCode=' + this.companyCode + '&phone=' + this.companyCode+ '&company_name=' + name + '&booth=' + booth
							});

						} else {
							console.log("触发失败");
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 2000
							});
						}
					},
					fail:()=>{ 
						console.log("无效的响应");
						console.log(this.url);
						
						}
					
					
					
				});


			},
			gz() {
				uni.navigateTo({
					url: '../signin/signin?openid=' + this.openid
				});
			},
			test() {
				uni.navigateTo({
					url: '/pages/checkin/checkin'
				})
			}
		},
		

	}
</script>

<style>
.form-company{
	display: flex;
	flex-direction: column;
	justify-content: center;
}

.form-company input{
	border: solid 1px #efefef;
	height: 32px;
	width: 80%;
	margin: 0 auto;
	padding-left: 15px;
	margin-top: 55rpx;
}


.title{width: 80%;
	margin: 0 auto;margin-top: 50px;font-weight: 600;color: #eb6100;}
	
	
	
.content{width: 80%; margin: 0 auto;margin-top: 10px; font-size: 24rpx;color: darkslategrey; }



button{
	width: 189.84rpx;
	height: 75rpx;
	line-height: 75rpx;
	margin-top: 20px;
	background-color: #eb6100;
	color: #fff;
}
</style>
